//
//  AMPSADJGBannerAdapter.m
//  AdScopeDemo
//
//  Created by apple on 2025/5/22.
//

#import "AMPSADJGBannerAdapter.h"
#import <ADJgSDK/ADJgSDKBannerAdView.h>
#import <AdScopeFoundation/AdScopeFoundation.h>
#import "AMPSADJGSDKManagerAdapter.h"

@interface AMPSADJGBannerAdapter () <ADJgSDKBannerAdViewDelegate>

@property (nonatomic, strong) ADJgSDKBannerAdView *unifiedBannerView;

@property (nonatomic, assign) BOOL isBiddingRequest;

@property (nonatomic, assign) BOOL isBiddingCallBack;

@property (nonatomic, strong) AdScopeTimer *timeoutTimer;

@property (nonatomic, strong) AMPSAdConfiguration *adConfiguration;

@end

@implementation AMPSADJGBannerAdapter

- (void)requestBiddingPriceWithSpaceId:(NSString *)spaceId
                       adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.isBiddingRequest = YES;
    [self loadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
}

- (void)loadAdWithSpaceId:(nonnull NSString *)spaceId adConfiguration:(nonnull AMPSAdConfiguration *)adConfiguration {
    if (AMPSADJGSDKManagerAdapter.initState != kAdScopeMediationAdapterInitSDKStateSuccess) {
        __weak typeof(self)weakSelf = self;
        [AMPSADJGSDKManagerAdapter startAsyncWithAppId:self.appId configuration:self.sdkConfiguration results:^(BOOL adapterResult) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (adapterResult) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
                });
            } else {
                if (strongSelf.isBiddingRequest) {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:250401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.bidDelegate && [strongSelf.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
                        [strongSelf.bidDelegate requestBiddingPriceFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:270401 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGSDKManagerAdapter init fail"}];
                    if (strongSelf.customDelegate && [strongSelf.customDelegate respondsToSelector:@selector(bannerAdLoadFail:error:ext:)]) {
                        [strongSelf.customDelegate bannerAdLoadFail:strongSelf error:error ext:adConfiguration.ext];
                    }
                }
            }
        }];
    } else {
        [self gdtLoadAdWithSpaceId:spaceId adConfiguration:adConfiguration];
    }
}

- (void)gdtLoadAdWithSpaceId:(NSString *)spaceId adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    self.unifiedBannerView = [[ADJgSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.width, adConfiguration.adSize.height)];
    self.unifiedBannerView.delegate = self;
    self.unifiedBannerView.posId = spaceId;
    self.adConfiguration = adConfiguration;
    CGFloat floatValue = adConfiguration.timeoutInterval/1000 > 0 ? adConfiguration.timeoutInterval/1000 : 5;
    self.timeoutTimer = [AdScopeTimer adScopeScheduledWeakTimerWithTimeInterval:floatValue target:self selector:@selector(timeoutTimerSelector) repeats:NO];
    [self.unifiedBannerView loadAndShow:self.viewController];
}

- (void)timeoutTimerSelector {
//    NSError *error = [NSError errorWithDomain:@"xyz.adscope.MediationErrorDomain" code:-9999 userInfo:@{@"NSLocalizedDescription": @"AMPSADJGBannerAdapter load ad timeout"}];
    [self adjg_bannerViewFailToReceived:self.unifiedBannerView errorModel:[ADJgAdapterErrorDefine new]];
}

- (void)removeAd {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    if (_unifiedBannerView) {
        _unifiedBannerView.delegate = nil;
        _unifiedBannerView = nil;
    }
}

- (BOOL)isValid {
    return self.isLoadAdSuccess;;
}

- (void)sendLossNotificationWithInfo:(nonnull NSDictionary *)lossInfo {
   
}

- (void)sendWinNotificationWithInfo:(nonnull NSDictionary *)winInfo {
    
}

- (void)loadC2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    if (self.isLoadAdSuccess) {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdLoadSuccess:ext:)]) {
            [self.customDelegate bannerAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
}

- (void)loadS2SAdWithSpaceId:(NSString *)spaceId
                biddingToken:(id)biddingToken
             adConfiguration:(AMPSAdConfiguration *)adConfiguration {
    
}

- (NSInteger)eCPM {
    ADJgSDKExtInfo *extInfo = [self.unifiedBannerView adjg_extInfo];
    return [extInfo.ecpm integerValue];
}

- (void)adjg_bannerViewDidReceived:(ADJgSDKBannerAdView *)bannerView {
    if (_timeoutTimer) {
        [_timeoutTimer invalidate];
        _timeoutTimer = nil;
    }
    self.bannerView = bannerView;
    self.isLoadAdSuccess = YES;
    if (self.isBiddingRequest && !self.isBiddingCallBack) {
        self.isBiddingCallBack = YES;
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceSuccess:ext:)]) {
            [self.bidDelegate requestBiddingPriceSuccess:self ext:self.adConfiguration.ext];
        }
    } else {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdLoadSuccess:ext:)]) {
            [self.customDelegate bannerAdLoadSuccess:self ext:self.adConfiguration.ext];
        }
    }
}

- (void)adjg_bannerViewFailToReceived:(ADJgSDKBannerAdView *)bannerView errorModel:(ADJgAdapterErrorDefine *)errorModel {
    NSError *tempError = ADJgAdapterError(ADJgAdapterErrorDomain, errorModel.code, errorModel.message);
    if (self.isBiddingRequest && !self.isBiddingCallBack) {
        if (self.bidDelegate && [self.bidDelegate respondsToSelector:@selector(requestBiddingPriceFail:error:ext:)]) {
            self.isBiddingCallBack = YES;
            [self.bidDelegate requestBiddingPriceFail:self error:tempError ext:self.adConfiguration.ext];
        }
    } else {
        if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdLoadFail:error:ext:)]) {
            [self.customDelegate bannerAdLoadFail:self error:tempError ext:self.adConfiguration.ext];
        }
    }
    if (!self.isLoadAdSuccess) {
        [self removeAd];
    }
}

- (void)adjg_bannerViewDidPresent:(nonnull ADJgSDKBannerAdView *)bannerView {
    
}

- (void)adjg_bannerViewExposure:(ADJgSDKBannerAdView *)bannerView {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdDidShow:)]) {
        [self.customDelegate bannerAdDidShow:self];
    }
}

- (void)adjg_bannerViewClicked:(ADJgSDKBannerAdView *)bannerView {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdDidClick:)]) {
        [self.customDelegate bannerAdDidClick:self];
    }
}

- (void)adjg_bannerViewClose:(ADJgSDKBannerAdView *)bannerView {
    if (self.customDelegate && [self.customDelegate respondsToSelector:@selector(bannerAdDidClose:)]) {
        [self.customDelegate bannerAdDidClose:self];
    }
    [self removeAd];
}

- (void)adjg_bannerAdCloseLandingPage:(nonnull ADJgSDKBannerAdView *)bannerView { 
    
}

- (void)dealloc {
    [self removeAd];
}

@end
